﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Threading;
using FinancialManagement.Models.Repository;
using FinancialManagement.Models;
using Telerik.Web.Mvc;
using FinancialManagement.Utils;

namespace FinancialManagement.Controllers
{
    /// <summary>
    /// 预算管理
    /// </summary>
    public class BudgetController : Controller
    {
        private AccountRepository accountRepository = new AccountRepository();
        private BudgetRepository budgetRepository = new BudgetRepository();
        //
        // GET: /Budget/

        public ActionResult Index()
        {
            var isFinancial = accountRepository.GetRoleAdminByUserID(User.Identity.Name);
            ViewData["admin"] = isFinancial;

            ViewData["UserName"] = accountRepository.GetUserNameByUserID(User.Identity.Name);

            ViewBag.DepName = accountRepository.GetDepNameByUserID(User.Identity.Name);
            ViewData["id"] = "";
            ViewBag.WindowTitle = "";


            var canBudget = (bool)Session["canBudget"];
            if (canBudget)
            {
                return View();
            }
            else
            {
                return View("Error");
            }

            
        }

        /// <summary>
        /// 显示添加项目预算明细信息的Window
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShowAddWindow(string id)
        {
            ViewBag.StrLen = "(200字以内)";
            ViewBag.WindowTitle = "添加申报预算明细";
            ProjectTbaleDetails detail = new ProjectTbaleDetails();
            detail.ProjectID = id;
            return View(detail);
        }

        /// <summary>
        /// 显示修改项目预算明细信息的Window
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShowEditWindow(int id)
        {
            ViewBag.StrLen = "(200字以内)";
            ViewBag.WindowTitle = "更新申报预算明细";
            ProjectTbaleDetails detail = budgetRepository.GetDepBudgetProjectDetailByDSID(id);
            return View(detail);
        }

        /// <summary>
        /// 插入项目预算明细信息
        /// </summary>
        /// <returns></returns>
        public string InsertDepBudgetProjectDetail(ProjectTbaleDetails detail)
        {
            var model = new ProjectTbaleDetails();
            if (TryUpdateModel(model))
            {
                model.Illustration = UploadController.FilePath;

                model.Annual = Util.GetAnnualYear(Util.RevenueType.Budget);

                //The model is valid - insert the product.                
                var isOk = budgetRepository.InsertDepBudgetProjectDetail(model);
                if (isOk)
                {
                    return Util.SUCCESS;
                }
                return Util.ERROR;
            }
            return Util.ERROR;
        }

        /// <summary>
        /// 更新项目预算明细信息
        /// </summary>
        /// <param name="details"></param>
        /// <returns></returns>
        public string SaveDepBudgetProjectDetail(ProjectTbaleDetails details)
        {
            var model = budgetRepository.GetDepBudgetProjectDetailByDSID(details.DSID);
            TryUpdateModel(model);
            var isOk = budgetRepository.UpdateDepBudgetProjectDetail(model);
            if (isOk)
            {
                return Util.SUCCESS;
            }
            return Util.ERROR;
        }

        /// <summary>
        /// 删除项目预算明细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public string DeleteDepBudgetProjectDetail(int id)
        {
            var isOk = budgetRepository.DeleteDepBudgetProjectDetailByDSID(id);
            if (isOk)
            {
                return Util.SUCCESS;
            }
            return Util.ERROR;
        }

        /// <summary>
        /// 加载id=Details的Grid
        /// </summary>
        /// <param name="projectID"></param>
        /// <returns></returns>
        [GridAction]
        public ActionResult SelectionClientSide_ProjectDetails(string projectID)
        {
            projectID = projectID ?? "";
            return View(new GridModel(budgetRepository.GetDepBudgetProjectDetailByProjectId(projectID)));
        }

        /// <summary>
        /// 加载id=Project的Grid
        /// </summary>
        /// <returns></returns>
        [GridAction]
        public ActionResult SelectionClientSide_ProjectTables()
        {
            string depId = accountRepository.GetDepIdByUserID(User.Identity.Name);
            return View(new GridModel(budgetRepository.GetDepBudgetCollectionsByDepId(depId)));
        }
    }
}
